package leetcode;

/**
 * @author yujiafan
 * @version 1.0.0
 * @create 2022-12-12
 */
public class LeetCode9 {

    public static boolean isPalindrome(int x) {
        if(x < 0){
            return false;
        }
        if(x < 10){
            return true;
        }
        // 判断x由几位组成
        int bit = 0;
        int temp = x;
        do {
            temp = temp / 10;
            bit++;
        } while (temp != 0);
        // 用数组存储每一位的信息
        boolean flag = true;
        int y,z;
        for (int i = 1; i <= bit / 2; i++) {
            y = (int) ((x / (Math.pow(10,bit-i))) % 10);
            z = (int) ((x / (Math.pow(10,i-1))) % 10);
            if(y != z) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static boolean isPalindrome1(int x){
        if(x < 0 || (x % 10 == 0 && x != 0)){
            return false;
        }
        int temp = 0;
        while (x > temp){
            temp = temp * 10 + x % 10;
            x /= 10;
        }
        return x == temp || x == temp / 10;
    }

    public static void main(String[] args) {
        boolean palindrome = isPalindrome1(1001);
        System.out.println(palindrome);
    }
}
